<?php require_once("Connections/cotiza.php"); ?>
<?php require_once("include/Quick_CSV_import.php"); ?>
<?php
	$user     = $_POST['user'];
	$pass     = md5($_POST['pass']);
	$ambiente = $_POST['ambiente'];
	$query = "SELECT * FROM usuarios WHERE usuario = '$user' AND passwd = '$pass'";

	if($ambiente == "pilo")
	{
		mysql_select_db($database_cotiza_pilo, $cotiza);
		$users = mysql_query($query, $cotiza) or die(mysql_error());
		$row = mysql_fetch_assoc($users);
		$totalRows = mysql_num_rows($users);
		
		if($totalRows == 1)
		{
			session_start();
			if($row['activo'] == 1)
			{
				$_SESSION['usuario']     = $row['usuario'];
				$_SESSION['nombre']      = $row['nombre'];
				$_SESSION['permisos']    = $row['permisos'];
				$_SESSION['id_qad']      = $row['qad'];
				$_SESSION['autenticado'] = 1;
				
				echo $_SESSION['usuario']."<br>";
				echo $_SESSION['nombre']."<br>";
				echo $_SESSION['permisos']."<br>";
				echo $_SESSION['id_qad']."<br>";
				echo $_SESSION['autenticado'];
								
				/*
				shell_exec("/u1/users/pfs/cotizador/load_cmmstr_pilo.sh");
				shell_exec("/u1/users/pfs/cotizador/load_admstr.sh");
				shell_exec("/u1/users/pfs/cotizador/load_ctmstr.sh");
				shell_exec("/u1/users/pfs/cotizador/load_tcambio.sh");

				$query = "TRUNCATE cm_mstr";
				mysql_query($query,$cotiza) or die(mysql_error());
				
				$query = "TRUNCATE ad_mstr";
				mysql_query($query,$cotiza);
				
				$query = "TRUNCATE ct_mstr";
				mysql_query($query,$cotiza);
				
				$query = "TRUNCATE moneda_mstr";
				mysql_query($query,$cotiza);
				
				$csv = new Quick_CSV_import("/u1/apache/htdocs/cotizador/prod/data/cm_mstr.csv");
				$csv->table_name = "cm_mstr";
				$csv->encoding = "latin1";
				$csv->import();
				
				$csv2 = new Quick_CSV_import("/u1/apache/htdocs/cotizador/prod/data/ad_mstr.csv");
				$csv2->table_name = "ad_mstr";
				$csv->encoding = "latin1";
				$csv2->import();
				
				$csv2 = new Quick_CSV_import("/u1/apache/htdocs/cotizador/prod/data/ct_mstr.csv");
				$csv2->table_name = "ct_mstr";
				$csv->encoding = "latin1";
				$csv2->import();
				
*/
				$csv2 = new Quick_CSV_import("/u1/apache/htdocs/cotizador/prod/data/tipo_cambio.csv");
				$csv2->table_name = "moneda_mstr";
				$csv->encoding = "latin1";
				$csv2->import();
				header("Location: pilo/main.php");
			}
			else
				header("Location: index.php?err=2");
		}
		else
			header("Location: index.php?err=1");
		
	}//if($ambiente = "pilo")
	elseif($ambiente == "prod")
	{
		mysql_select_db($database_cotiza, $cotiza);
		$users = mysql_query($query, $cotiza) or die(mysql_error());
		$row = mysql_fetch_assoc($users);
		$totalRows = mysql_num_rows($users);
		
		if($totalRows == 1)
		{
			session_start();
			if($row['activo'] == 1)
			{
				$_SESSION['usuario']     = $row['usuario'];
				$_SESSION['nombre']      = $row['nombre'];
				$_SESSION['permisos']    = $row['permisos'];
				$_SESSION['id_qad']      = $row['qad'];
				$_SESSION['autenticado'] = 1;

				shell_exec("/u1/users/pfs/cotizador/load_cmmstr.sh");
				shell_exec("/u1/users/pfs/cotizador/load_admstr.sh");
				shell_exec("/u1/users/pfs/cotizador/load_ctmstr.sh");
				shell_exec("/u1/users/pfs/cotizador/load_tcambio.sh");
				

				$query = "TRUNCATE cm_mstr";
				mysql_query($query,$cotiza) or die(mysql_error());

				$query = "TRUNCATE ad_mstr";
				mysql_query($query,$cotiza);
				
				$query = "TRUNCATE ct_mstr";
				mysql_query($query,$cotiza);
				
				$query = "TRUNCATE moneda_mstr";
				mysql_query($query,$cotiza);

				$csv = new Quick_CSV_import("/u1/apache/htdocs/cotizador/prod/data/cm_mstr.csv");
				$csv->table_name = "cm_mstr";
				$csv->encoding = "latin1";
				$csv->import();

				$csv2 = new Quick_CSV_import("/u1/apache/htdocs/cotizador/prod/data/ad_mstr.csv");
				$csv2->table_name = "ad_mstr";
				$csv->encoding = "latin1";
				$csv2->import();
				
				$csv2 = new Quick_CSV_import("/u1/apache/htdocs/cotizador/prod/data/ct_mstr.csv");
				$csv2->table_name = "ct_mstr";
				$csv->encoding = "latin1";
				$csv2->import();
				
				$csv2 = new Quick_CSV_import("/u1/apache/htdocs/cotizador/prod/data/tipo_cambio.csv");
				$csv2->table_name = "moneda_mstr";
				$csv->encoding = "latin1";
				$csv2->import();

				//shell_exec("/u1/users/pfs/cotizador/load_cmmstr.sh");
				//$csv = new Quick_CSV_import("c:\\cm_mstr.csv");
				//$csv->table_name = "cm_mstr";
				//$csv->import();
				header("Location: prod/main.php");
			}
			else
				header("Location: index.php?err=2");
		}
		else
			header("Location: index.php?err=1");

	}//elseif($ambiente == "prod")
?>
